package com.wen.wenda.service.scoring;

import com.wen.wenda.annotation.ScoringStrategyConfig;
import com.wen.wenda.common.BaseCode;
import com.wen.wenda.exception.BusinessException;
import com.wen.wenda.model.domain.App;
import com.wen.wenda.model.domain.UserAnswer;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评分策略执行器
 *
 * @author wen
 */
@Service
public class ScoringStrategyExecutor {

    @Resource
    private List<ScoringStrategy> scoringStrategyList;

    /**
     * 策略模式执行评分
     *
     * @param choices 用户答案列表
     * @param app 应用
     * @return 结果
     */
    public UserAnswer executeScoring(List<String> choices, App app) {
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if (appType == null || scoringStrategy == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR, "应用参数有误");
        }
        // 根据注解获取策略
        for (ScoringStrategy strategy : scoringStrategyList) {
            if (strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)) {
                ScoringStrategyConfig scoringStrategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (scoringStrategyConfig.appType() == appType && scoringStrategyConfig.scoringStrategy() == scoringStrategy) {
                    return strategy.executeScoring(choices, app);
                }
            }
        }
        throw new BusinessException(BaseCode.INTERNAL_ERROR);
    }
}
